<?php

namespace common\models\user;

use Yii;

/**
 * This is the model class for table "{{%user_address}}".
 *
 * @property string $id
 * @property string $user_id 用户UID
 * @property string $name 收件人姓名
 * @property string $mobile 收件人手机号
 * @property string $province 省份
 * @property string $city 城市
 * @property string $district 区县
 * @property string $address_detail 详细地址
 * @property string $area_code 地区编码
 * @property string $postal_code 邮政编码
 * @property int $is_default 默认地址
 * @property string $created_at 地址添加时间
 * @property string $updated_at 最近更新时间
 */
class UserAddress extends \common\db\ActiveRecord {

    /**
     * 默认地址
     */
    const IS_DEFAULT_YES = 1;

    /**
     * 普通地址
     */
    const IS_DEFAULT_NO = 0;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%user_address}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'is_default', 'created_at', 'updated_at'], 'integer'],
            [['name', 'mobile', 'province', 'city', 'district', 'address_detail'], 'required'],
            [['name', 'province'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 11],
            [['city', 'district'], 'string', 'max' => 30],
            [['address_detail'], 'string', 'max' => 255],
            [['area_code', 'postal_code'], 'string', 'max' => 6],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户UID',
            'name' => '收件人姓名',
            'mobile' => '收件人手机号',
            'province' => '省份',
            'city' => '城市',
            'district' => '区县',
            'address_detail' => '详细地址',
            'area_code' => '地区编码',
            'postal_code' => '邮政编码',
            'is_default' => '默认地址',
            'created_at' => '地址添加时间',
            'updated_at' => '最近更新时间',
        ];
    }

    public function getAddress() {
        return $this->province . $this->city . $this->district . ' ' . $this->address_detail;
    }

    public function getUsing() {
        return $this->hasMany(\common\models\freight\FreightReceiver::className(), ['original_id' => 'id']);
    }

    public function getUsedCount() {
        return $this->getUsing()->count();
    }

    public function getTag() {
        return $this->is_default ? '默认' : null;
    }

    /**
     * @inheritdoc
     */
    public function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);
        if ($this->is_default) {
            static::updateAll(['is_default' => static::IS_DEFAULT_NO], [
                'and',
                ['user_id' => $this->user_id],
                ['!=', 'id', $this->id]
            ]);
        }
    }

    public function getIsDefault() {
        return (bool) $this->is_default;
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'mobile',
            'province',
            'city',
            'district',
            'address' => 'address_detail',
            'default' => 'isDefault',
            'tag'
        ];
    }

}
